package pers.daisp.book;

import java.io.*;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Socket;

public class Client {

    public static void main(String[] args) throws IOException {
        Socket socket = new Socket();
        socket.setSoTimeout(3000);
        socket.connect(new InetSocketAddress(InetAddress.getLocalHost(), 2000), 3000);

        log("连接上服务端成功，服务端地址：" + socket.getInetAddress().getHostAddress() + ":" + socket.getPort());
        log("本地地址：" + socket.getLocalAddress().getHostAddress() + ":" + socket.getLocalPort());
        try {
            message(socket);
        } catch (Exception e) {
            log(e.getMessage());
        }

        socket.close();
        log("客户端已退出");
    }

    private static void message(Socket socket) throws IOException {
        //获取键盘输入流
        InputStream inputStream = System.in;
        BufferedReader input = new BufferedReader(new InputStreamReader(inputStream));

        //socket输出流
        OutputStream outputStream = socket.getOutputStream();
        PrintStream socketWriter = new PrintStream(outputStream);

        //socket输入流
        InputStream socketInputStream = socket.getInputStream();
        BufferedReader socketReader = new BufferedReader(new InputStreamReader(socketInputStream));


        while (true) {
            String message = input.readLine();
            socketWriter.println(message);
            log("发送消息成功：" + message);

            String ackMessage = socketReader.readLine();
            log("收到消息：" + ackMessage);
            if ("bye".equals(ackMessage))  {
                break;
            }
        }
        input.close();
        socketWriter.close();
        socketReader.close();
    }

    private static void log(String msg) {
        System.out.println(msg);
    }
}
